package com.heu.blood.common.commponent;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.dict.entity.DictDataEntity;
import com.heu.blood.dict.service.DictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class DictionaryLoader {
// 返回的是<dict_type,<dict_code, [dict_value, dict_appendix]>>
//    比如<PaymentProject,<hb,[血红蛋白, 50元/u]>>
    private Map<String, Map<String, String[]>> dict;
//    subDict和valToCode指的是根据val去求其key，即根据血红蛋白取出hb，在test有测试
    private Map<String,String> subDict;

    private Map<String, String> valToCode;
    // 血液翻译的字典
    private Map<String,String> bloodTypeDictTranslate = new HashMap<>();

    private Map<String,String> measurementUnitDictTranslate = new HashMap<>();
    // 不同类型血液配血价格字典
    private Map<String,String> bloodTypeCostDictTranslate = new HashMap<>();
    // 不同配血方法的价格字典
    private Map<String,String> paymentProjectTranslate = new HashMap<>();

    @Autowired
    private DictDataService dictDataService;

    @PostConstruct
    public void setDict(){
        this.dict = dictDataService.getAllDictData();
    }
    @PostConstruct
    public void setSubDict() {
        this.subDict = dictDataService.getDictAndCode();
    }

    public Map<String, String> getValToCode(){
        return this.valToCode;
    }

    @PostConstruct
    public void setDictValueToCode(){
        this.valToCode = dictDataService.getValToCode();
    }
    @PostConstruct
    public void setBloodTypeDictTranslate(){
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictDataEntity::getDictType,"BloodType");
        // 找到所有的血液类型的实体
        List<DictDataEntity> list = dictDataService.list(queryWrapper);
        // 遍历并保存
        for(DictDataEntity dictDataEntity : list){
            this.bloodTypeDictTranslate.put(dictDataEntity.getDictValue(),dictDataEntity.getDictCode());
            this.bloodTypeCostDictTranslate.put(dictDataEntity.getDictCode(),dictDataEntity.getDictAppendix().split("元")[0]);
        }
        log.info("完成加载血液类型字典数据----------------------------------------------------");
    }
    @PostConstruct
    public void setMeasurementUnitDictTranslate(){
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictDataEntity::getDictType,"MeasurementUnit");
        // 找到所有的血液类型的实体
        List<DictDataEntity> list = dictDataService.list(queryWrapper);
        // 遍历并保存
        for(DictDataEntity dictDataEntity : list){
            this.measurementUnitDictTranslate.put(dictDataEntity.getDictValue(),dictDataEntity.getDictCode());
        }
        log.info("完成加载血液计量单位字典数据----------------------------------------------------");
    }
    @PostConstruct
    public void setPaymentProjectTranslate(){
        LambdaQueryWrapper<DictDataEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictDataEntity::getDictType,"PaymentProject");
        // 找到所有的血液类型的实体
        List<DictDataEntity> list = dictDataService.list(queryWrapper);
        // 遍历并保存
        for(DictDataEntity dictDataEntity : list){
            this.paymentProjectTranslate.put(dictDataEntity.getDictCode(),dictDataEntity.getDictAppendix().split("元")[0]);
        }
        log.info("完成加载配血方法数据----------------------------------------------------");
    }

    // 提供访问字典数据的方法
    public Map<String, Map<String, String[]>> getDict(){
        return this.dict;
    }

    public Map<String,String> getSubDict(){
        return this.subDict;
    }

    public Map<String,String> getBloodTypeDictTranslate(){
        return this.bloodTypeDictTranslate;
    }

    public Map<String,String> getBloodTypeDictCostTranslate(){
        return this.bloodTypeCostDictTranslate;
    }

    public Map<String,String> getMeasurementUnitDictTranslate(){
        return this.measurementUnitDictTranslate;
    }

    public Map<String,String> getPaymentProjectTranslate(){ return this.paymentProjectTranslate; }
}
